package com.sinvo.oa.controller;


import com.sinvo.oa.entity.model.system.SysDept;
import com.sinvo.oa.exception.CustomException;
import com.sinvo.oa.log.annotation.Log;
import com.sinvo.oa.log.enums.ServiceType;
import com.sinvo.oa.result.R;
import com.sinvo.oa.result.ResultCode;
import com.sinvo.oa.service.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 部门控制器
 */
@Api(tags = "部门管理接口")
@RestController
@RequestMapping("admin/system/sysDept")
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 查询所有部门 树形结构
     * @return R
     */
    @ApiOperation(value = "部门树形列表")
    @GetMapping("findNodes")
    public R findNodes(){
        List<SysDept> deptList = sysDeptService.findAllDeptNodes();
        return R.success(deptList);
    }

    /**
     * 新增部门
     * @param sysDept
     * @param result
     * @return
     */
    @Log(title = "部门管理",businessType = ServiceType.INSERT)
    @PreAuthorize("hasAuthority('btn.sysDept.add')")
    @ApiOperation(value = "新增部门")
    @PostMapping("save")
    public R save(@Validated @RequestBody SysDept sysDept, BindingResult result) {
        //参数校验
        if (result.hasErrors()) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(), "非法参数");
        }
        sysDeptService.saveDept(sysDept);
        return R.success();
    }

    /**
     * 更新部门
     * @param sysDept
     * @param result
     * @return
     */
    @Log(title = "部门管理",businessType = ServiceType.UPDATE)
    @PreAuthorize("hasAuthority('btn.sysDept.update')")
    @ApiOperation(value = "修改部门")
    @PutMapping("update")
    public R update(@Validated @RequestBody SysDept sysDept, BindingResult result) {
        //参数校验
        if (result.hasErrors()) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(), "非法参数");
        }
        sysDeptService.updateDept(sysDept);
        return R.success();
    }

    /**
     * 移除部门
     * @param id
     * @return
     */
    @Log(title = "部门管理",businessType = ServiceType.DELETE)
    @PreAuthorize("hasAuthority('btn.sysMenu.remove')")
    @ApiOperation(value = "删除部门")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable Long id) {
        //验证参数
        if (StringUtils.isEmpty(id)) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(), "非法参数");
        }
        sysDeptService.removeDeptById(id);
        return R.success();
    }

    @Log(title = "部门管理",businessType = ServiceType.UPDATE)
    @PreAuthorize("hasAuthority('btn.sysDept.update')")
    @ApiOperation(value = "更新菜单状态")
    @GetMapping("updateStatus/{id}/{status}")
    public R updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        //验证参数 id为空或者status不等于1或0 提示错误
        if (StringUtils.isEmpty(id) || !(status == 1 || status == 0)) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(), "非法参数");
        }
        boolean success = sysDeptService.changeStatus(id, status);
        if (!success) {
            throw new CustomException(ResultCode.SERVICE_ERROR.getCode(), "修改失败，请重试");
        }
        return R.success();
    }
}

